home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 3 / adb / g-htable < prev    next >
Text File  |  1996-02-12  |  6KB  |  203 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                          G N A T . H T A B L E                           --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.3 $                              --
  10. --                                                                          --
  11. --     Copyright (C) 1992,1993,1994,1995 Free Software Foundation, Inc.     --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. package body Gnat.Htable is
  37.  
  38.    --------------------
  39.    --  Static_Htable --
  40.    --------------------
  41.  
  42.    package body Static_Htable is
  43.  
  44.       Table : array (Header_Num) of Elmt_Ptr;
  45.  
  46.       ---------
  47.       -- Set --
  48.       ---------
  49.  
  50.       procedure Set (E : Elmt_Ptr) is
  51.          Index : Header_Num;
  52.  
  53.       begin
  54.          Index := Hash (Get_Key (E));
  55.          Set_Next (E, Table (Index));
  56.          Table (Index) := E;
  57.       end Set;
  58.  
  59.       ---------
  60.       -- Get --
  61.       ---------
  62.  
  63.       function  Get (K : Key) return Elmt_Ptr is
  64.          Elmt  : Elmt_Ptr;
  65.  
  66.       begin
  67.          Elmt := Table (Hash (K));
  68.  
  69.          loop
  70.             if Elmt = null then
  71.                return null;
  72.  
  73.             elsif Equal (Get_Key (Elmt), K) then
  74.                return Elmt;
  75.  
  76.             else
  77.                Elmt := Next (Elmt);
  78.             end if;
  79.          end loop;
  80.       end Get;
  81.  
  82.       ------------
  83.       -- Remove --
  84.       ------------
  85.  
  86.       procedure Remove  (K : Key) is
  87.          Index     : constant Header_Num := Hash (K);
  88.          Elmt      : Elmt_Ptr;
  89.          Next_Elmt : Elmt_Ptr;
  90.  
  91.       begin
  92.          Elmt := Table (Index);
  93.  
  94.          if Elmt = null then
  95.             return;
  96.  
  97.          elsif Equal (Get_Key (Elmt), K) then
  98.             Table (Index) := Next (Elmt);
  99.  
  100.          else
  101.             loop
  102.                Next_Elmt :=  Next (Elmt);
  103.  
  104.                if Next_Elmt = null then
  105.                   return;
  106.  
  107.                elsif Equal (Get_Key (Next_Elmt), K) then
  108.                   Set_Next (Elmt, Next (Next_Elmt));
  109.                   return;
  110.  
  111.                else
  112.                   Elmt := Next_Elmt;
  113.                end if;
  114.             end loop;
  115.          end if;
  116.       end Remove;
  117.    end Static_Htable;
  118.  
  119.    --------------------
  120.    --  Simple_Htable --
  121.    --------------------
  122.  
  123.    package body Simple_Htable is
  124.  
  125.       type Element_Wrapper;
  126.       type Elmt_Ptr is access all Element_Wrapper;
  127.       type Element_Wrapper is record
  128.          K : Key;
  129.          E : Element;
  130.          Next : Elmt_Ptr;
  131.       end record;
  132.  
  133.       procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
  134.       function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
  135.       function Get_Key   (E : Elmt_Ptr) return Key;
  136.  
  137.       package Tab is new Static_Htable (
  138.         Header_Num => Header_Num,
  139.         Element    => Element_Wrapper,
  140.         Elmt_Ptr   => Elmt_Ptr,
  141.         Set_Next   => Set_Next,
  142.         Next       => Next,
  143.         Key        => Key,
  144.         Get_Key    => Get_Key,
  145.         Hash       => Hash,
  146.         Equal      => Equal);
  147.  
  148.       procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
  149.       begin
  150.          E.Next := Next;
  151.       end Set_Next;
  152.  
  153.       function Next (E : Elmt_Ptr) return Elmt_Ptr is
  154.       begin
  155.          return E.Next;
  156.       end Next;
  157.  
  158.       function Get_Key (E : Elmt_Ptr) return Key is
  159.       begin
  160.          return E.K;
  161.       end Get_Key;
  162.  
  163.       procedure Set (K : Key; E : Element) is
  164.          Tmp : Elmt_Ptr := Tab.Get (K);
  165.  
  166.       begin
  167.          if Tmp = null then
  168.             Tab.Set (new Element_Wrapper'(K, E, null));
  169.          else
  170.             Tmp.E := E;
  171.          end if;
  172.       end Set;
  173.  
  174.       function  Get (K : Key) return Element is
  175.          Tmp : Elmt_Ptr := Tab.Get (K);
  176.  
  177.       begin
  178.          if Tmp = null then
  179.             return No_Element;
  180.          else
  181.             return Tab.Get (K).E;
  182.          end if;
  183.       end Get;
  184.    end Simple_Htable;
  185.  
  186.    ----------
  187.    -- Hash --
  188.    ----------
  189.  
  190.    function Hash (Key : String) return Header_Num is
  191.       type S is mod 2**8;
  192.       Size : constant S := S (Header_Num'Last - Header_Num'First + 1);
  193.       Tmp  : S := 0;
  194.  
  195.    begin
  196.       for J in Key'Range loop
  197.          Tmp := Tmp xor S (Character'Pos (Key (J)));
  198.       end loop;
  199.       return Header_Num'First + Header_Num'Base (Tmp mod Size);
  200.    end Hash;
  201.  
  202. end Gnat.Htable;
  203.